<?php
/* * 注意此文档为UTF-8的编码.
 * Created on 2010-5-29
 *
 * @author EXT <isrn@qq.com>
 *
 */
include ('../../includes/global.php');
include ('../global.php');
include ("../power.php");
switch ($_POST['op']) {
    case 'list': {
            $node = $_POST['node'];
            $temp = Dgd(array(
                'db' => 'd_sevenel',
                'cd' => "del!=1 and parentid = '" . $node . "' order by px asc",
                'tn' => "tree"
                    ));
            foreach ($temp as $key => $value) {
                $treetemp['leaf'] = $value['leaf'] == 0 ? false : true;
                if ($value['cls']) {
                    $treetemp['iconCls'] = 'i_' . substr($value['cls'], 0, -4);
                } else {
                    $treetemp['iconCls'] = $value['leaf'] == 0 ? 'folder' : 'file';
                }
                if ($value['url'] == '') {
                    $treetemp['url'] = 'form_render.php?id=' . $value['id'];
                }
                $treetemp['text'] = $value['id'] . $value['title'];
                $treetemp['id'] = $value['id'];
                $trees[] = $treetemp;
            }
            echo json_encode($trees);
            break;
        }
    case 'mainload': {
            $node = $_POST['node'];
            $temp = Dgd(array(
                'db' => 'd_sevenel',
                'cd' => "parentid = '" . $node . "' and del!=1 and is_show=1 order by px asc",
                'tn' => "tree"
                    ));
            //print_r($temp);
            if ($_SESSION['rcg'] == 'A') {
                foreach ($temp as $key => $value) {
                    $treetemp['leaf'] = $value['leaf'] == 0 ? false : true;
                    if ($value['cls']) {
                        $treetemp['iconCls'] = 'i_' . substr($value['cls'], 0, -4);
                    } else {
                        $treetemp['iconCls'] = $value['leaf'] == 0 ? 'folder' : 'file';
                    }
                    if ($value['url'] == '') {
                        $treetemp['action'] = '';
                    } else {
                        $treetemp['action'] = $value['url'];
                    }
                    $treetemp['text'] = $value['title'];
                    $treetemp['id'] = $value['id'];
                    $trees[] = $treetemp;
                }
                echo json_encode($trees); } else {
                foreach ($temp as $key => $value) {
                    if ($_SESSION['rcg']['xr'][$value['id']]) {
                        $treetemp['leaf'] = $value['leaf'] == 0 ? false : true;
                        if ($value['cls']) {
                            $treetemp['iconCls'] = 'i_' . substr($value['cls'], 0, -4);
                        } else {
                            $treetemp['iconCls'] = $value['leaf'] == 0 ? 'folder' : 'file';
                        }
                        if ($value['url'] == '') {
                            $treetemp['action'] = '';
                        } else {
                            $treetemp['action'] = $value['url'];
                        }
                        $treetemp['text'] = $value['title'];
                        $treetemp['id'] = $value['id'];
                        $lg = json_decode($value['config'], true);
                        $treetemp['config'] = $lg['lg'];
                        $trees[] = $treetemp;
                    }
                }
                echo json_encode($trees); }
            break;
        }
    case 'showparent': {
            error_reporting(0);
            $node = $_POST['node'];
            $temp = Dgd(array(
                'db' => 'd_sevenel',
                'cd' => "del!=1 and parentid = '" . $node . "' and leaf=0 order by px asc",
                'tn' => "tree"
                    ));
            foreach ($temp as $key => $value) {
                $value['leaf'] = $value['leaf'] == 0 ? false : true;
                $value['cls'] = 'folder';
                $value['text'] = $value['title'];
                $trees[] = $value;
            }
            echo json_encode($trees);
            break;
        }
    case 'px': {
            $pr = Dgd(array(
                'db' => 'd_sevenel',
                'cd' => "del!=1 and id = '" . $_POST['prid'] . "'",
                'tn' => "tree",
                'limit' => '1'
                    ));
            switch ($_POST['method']) {
                case 'append': {
                        $pr = Dgd(array(
                            'db' => 'd_sevenel',
                            'cd' => "del!=1 and parentid = '" . $pr['id'] . "'  order by px desc",
                            'tn' => "tree",
                            'limit' => '1'
                                ));
                        $d_sevenel->Desql("UPDATE se_tree SET  parentid='" . $_POST['prid'] . "', `px` = '" . ($pr['px'] + 1) . "' WHERE `se_tree`.`id` =" . $_POST['nowid'] . " LIMIT 1 ;");
                        break;
                    }
                case 'above': {
                        $prtemp = Dgd(array(
                            'db' => 'd_sevenel',
                            'cd' => "del!=1 and parentid = '" . $pr['parentid'] . "'  order by px asc",
                            'tn' => "tree"
                                ));
                        $num = 0;
                        foreach ($prtemp as $value) {
                            if ($value['id'] == $pr['id']) {
                                $d_sevenel->Desql("UPDATE se_tree SET  parentid='" . $pr['parentid'] . "', `px` = '" . (++$num) . "' WHERE `se_tree`.`id` =" . $_POST['nowid'] . " LIMIT 1 ;");
                                $d_sevenel->Desql("UPDATE se_tree SET   `px` = '" . (++$num) . "' WHERE `se_tree`.`id` =" . $value['id'] . " LIMIT 1 ;");
                            } else {
                                $value['id'] != $_POST['nowid'] && $d_sevenel->Desql("UPDATE se_tree SET   `px` = '" . (++$num) . "' WHERE `se_tree`.`id` =" . $value['id'] . " LIMIT 1 ;");
                            }
                        }
                        break;
                    }
                case 'below': {
                        $prtemp = Dgd(array(
                            'db' => 'd_sevenel',
                            'cd' => "del!=1 and parentid = '" . $pr['parentid'] . "'  order by px asc",
                            'tn' => "tree"
                                ));
                        $num = 0;
                        foreach ($prtemp as $value) {
                            if ($value['id'] == $pr['id']) {
                                $d_sevenel->Desql("UPDATE se_tree SET   `px` = '" . (++$num) . "' WHERE `se_tree`.`id` =" . $value['id'] . " LIMIT 1 ;");
                                $d_sevenel->Desql("UPDATE se_tree SET  parentid='" . $pr['parentid'] . "', `px` = '" . (++$num) . "' WHERE `se_tree`.`id` =" . $_POST['nowid'] . " LIMIT 1 ;");
                            } else {
                                $value['id'] != $_POST['nowid'] && $d_sevenel->Desql("UPDATE se_tree SET   `px` = '" . (++$num) . "' WHERE `se_tree`.`id` =" . $value['id'] . " LIMIT 1 ;");
                            }
                        }

                        break;
                    }
            }

            break;
        }
    case 'save_baseinfo': {

            !$_POST['shield_add'] && $_POST['shield_add'] = 0;
            !$_POST['shield_del'] && $_POST['shield_del'] = 0;
            !$_POST['del'] && $_POST['del'] = 0;
            !$_POST['shield_edit'] && $_POST['shield_edit'] = 0;
            !$_POST['is_expand'] && $_POST['is_expand'] = 0;
            !$_POST['is_show'] && $_POST['is_show'] = 0;
            $_POST['config'] = json_encode($_POST['config']);
            if ($_POST['id'] != '') {
                Du('sevenel', 'tree', $_POST, 'id', $_POST['id']);
                ext_request('ok');
            } else {
                Di('sevenel', 'tree', $_POST);
                ext_request('ok');
            }
            break;
        }
    case 'show_expand': {
            $temp = Dgd(array(
                'db' => 'd_sevenel',
                'cd' => "del!=1 and is_expand = 1",
                'tn' => "tree"
                    ));
            foreach ($temp as $arraw => $data) {
                $items_array[] = array('name' => $data['id'], 'value' => $data['title']);
            }
            $items_config = json_encode($items_array);
            echo $items_config;
            break; }
    case 'del': {
            Du('sevenel', 'tree', $_POST, 'id', $_POST['id']);
            ext_request('ok');
            break;
        }
}
?>
